<?php
namespace Home\Controller;
use Think\Controller;

class SelectController extends Controller {

  //查询供应表里所有信息
  public function QuerySupply(){
     //供应表
       $supply=M("Supplylist");
       $page=$_POST["page"];
       $data=$supply
       ->field("supplylist_id,t_user.user_nick,t_address.address_name,supplylist_goodsname,t_user.user_id,supplylist_price,
        supplylist_time,supplylist_desc,supplylist_image,supplylist_minnumber")
       ->join("t_user on t_supplylist.user_id =t_user.user_id ")
       ->join("t_address on t_supplylist.address_id=t_address.address_id")
       ->page("$page",6)
       ->select();
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
    }

    //查询用户id，所有我的采购信息
   public function QueryMyPurchase(){

       $userid=$_POST["userid"];
       $status=$_POST["status"];
       $user=M("User");
       $condition["t_mypurchase.user_id"]=$userid;
       $condition["t_mypurchase.mypurchase_status"]=$status;
       if ($condition==0) {

           $data=$user
           ->field("t_mypurchase.mypurchase_status,user_nick,t_address.address_name,t_mypurchase.needlist_id,t_mypurchase.user_id,
                    t_needlist.needlist_goodsname,t_needlist.needlist_number,
                    t_needlist.needlist_time,t_needlist.needlist_surplustime,
                    t_needlist.needlist_linkman,t_needlist.needlist_linkmanphone,
                    t_needlist.needlist_desc,t_goodsclassify.goodsclassify_name")
           ->join("t_mypurchase on t_mypurchase.user_id = t_user.user_id")
           ->join("t_address on t_user.address_id =t_address.address_id")
           ->join("t_needlist on t_mypurchase.needlist_id=t_needlist.needlist_id")
           ->join("t_goodsclassify on t_needlist.goodsclassify_id=t_goodsclassify.goodsclassify_id")
           ->where($condition)
           ->select();
           
           if ($data) {
              $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
           }else{
                $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
           }
       }else{
       $data=$user
       ->field("t_mypurchase.mypurchase_status,user_nick,t_address.address_name,t_mypurchase.needlist_id,t_mypurchase.user_id,
                t_needlist.needlist_goodsname,t_needlist.needlist_number,
                t_needlist.needlist_time,t_needlist.needlist_surplustime,
                t_needlist.needlist_linkman,t_needlist.needlist_linkmanphone,
                t_needlist.needlist_desc,t_goodsclassify.goodsclassify_name")
       ->join("t_mypurchase on t_mypurchase.user_id = t_user.user_id")
       ->join("t_address on t_user.address_id =t_address.address_id")
       ->join("t_needlist on t_mypurchase.needlist_id=t_needlist.needlist_id")
       ->join("t_goodsclassify on t_needlist.goodsclassify_id=t_goodsclassify.goodsclassify_id")
       ->where($condition)
       ->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }

    }
  }

    //根据用户id查询采购信息
   public function Purchase(){

       $userid=$_POST["userid"];

       $needlist=M("Needlist");

       $condition["user_id"]=$userid;
       //添加条件查询
       $data=$needlist->where($condition)->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
    }

    
    //水果
    public function Fruits(){

       $fruits=M("Supplylist");
       $page=$_POST["page"];
       //添加条件查询
       $data=$fruits
       ->field("supplylist_id,t_user.user_nick,t_user.user_id,t_address.address_name,supplylist_goodsname,supplylist_price,supplylist_time,supplylist_desc,supplylist_image,supplylist_minnumber")
       ->join("t_user on t_supplylist.user_id =t_user.user_id ")
       ->join("t_address on t_supplylist.address_id=t_address.address_id")
       ->where("goodsclassify_id=1")
       ->page("$page",6)
       ->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
       
    }

    //蔬菜
    public function Vegetables(){

       $vegetables=M("Supplylist");
       $page=$_POST["page"];
       //添加条件查询
       $data=$vegetables
       ->field("supplylist_id,t_user.user_nick,t_user.user_id,t_address.address_name,supplylist_goodsname,supplylist_price,supplylist_time,supplylist_desc,supplylist_image,supplylist_minnumber")
       ->join("t_user on t_supplylist.user_id =t_user.user_id ")
       ->join("t_address on t_supplylist.address_id=t_address.address_id")
       ->page("$page",6)
       ->where("goodsclassify_id=2")->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
       
    }

    //粮油
    public function Oils(){

       $Oils=M("Supplylist");
       $page=$_POST["page"];
       //添加条件查询
       $data=$Oils
       ->field("supplylist_id,t_user.user_nick,t_user.user_id,t_address.address_name,supplylist_goodsname,supplylist_price,supplylist_time,supplylist_desc,supplylist_image,supplylist_minnumber")
       ->join("t_user on t_supplylist.user_id =t_user.user_id ")
       ->join("t_address on t_supplylist.address_id=t_address.address_id")
       ->page("$page",6)
       ->where("goodsclassify_id=3")->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
       
    }


    //水产
    public function Fisheries(){

       $Fisheries=M("Supplylist");
       $page=$_POST["page"];
       //添加条件查询
       $data=$Fisheries
       ->field("supplylist_id,t_user.user_nick,t_user.user_id,t_address.address_name,supplylist_goodsname,supplylist_price,supplylist_time,supplylist_desc,supplylist_image,supplylist_minnumber")
       ->join("t_user on t_supplylist.user_id =t_user.user_id ")
       ->join("t_address on t_supplylist.address_id=t_address.address_id")
       ->page("$page",6)
       ->where("goodsclassify_id=4")->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
     
    }

    //禽畜
    public function Livestock(){

       $livestock=M("Supplylist");
       $page=$_POST["page"];
       //添加条件查询
       $data=$livestock
       ->field("supplylist_id,t_user.user_nick,t_user.user_id,t_address.address_name,supplylist_goodsname,supplylist_price,supplylist_time,supplylist_desc,supplylist_image,supplylist_minnumber")
       ->join("t_user on t_supplylist.user_id =t_user.user_id ")
       ->join("t_address on t_supplylist.address_id=t_address.address_id")
       ->page("$page",6)
       ->where("goodsclassify_id=5")->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
       
    }





    //用户取消采购删除按钮
      public function CancelPurchase(){

          $userid=$_POST["userid"];
          $needlistid=$_POST["needlistid"];
        
          $del = M("Mypurchase");
         

          $data=$del->where("user_id =$userid and needlist_id = $needlistid")->delete();
       
         if ($data==1) {
            $this->ajaxReturn(array("code"=>200,"message"=>"删除成功",$data),"JSON");
         }else{
             $this->ajaxReturn(array("code"=>500,"message"=>"删除失败"),"JSON");
         }
       
    }

    //为你推荐
      public function SuggestForYou(){

       $usersupply=M("Supplylist");

       $data=$usersupply->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }

          
    }

    //最新供应(从供应大厅获取其数据)
    public  function NewSupply(){

       $usersupply=M("Supplylist");
       
       $data=$usersupply->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
       
    }

    //轮播器图片
    public function Headimg(){

        $usersupply=M("Supplylist");
       
        $data=$usersupply
        ->field("supplylist_image")
        ->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
       
    }


    public function myPurchase(){
      $mypurchase=M("Mypurchase");
      $userid=$_POST["userid"];
      $mypurch=$_POST["status"];
      $needlistid=$_POST["needlistid"];

      $newdatas["mypurchase_status"]=$mypurch;
      $newdatas["user_id"]=$userid;
      $newdatas["needlist_id"]=$needlistid;
      $result=$mypurchase->add($newdatas);
       
}

    //发布采购
    public function AddNeed(){

      $addneeds=M("Needlist");

      $goodsname=$_POST["goodsname"];
      $number=$_POST["number"];
      $time=$_POST["time"];
      $surplustime=$_POST["plustime"];
      $linkman=$_POST["linkman"];
      $tel=$_POST["tel"];
      $desc=$_POST["desc"];
      $timeinterrval=$_POST["timeinterrval"];
       $userid=$_POST["userid"];
      $goodclassid=$_POST["goodclassid"];
      $goodid=$_POST["goodid"];
      $addressid=$_POST["addressid"];


      $newdata["needlist_goodsname"]=$goodsname;
      $newdata["needlist_number"]=$number;
      $newdata["needlist_time"]=$time;
      $newdata["needlist_surplustime"]=$surplustime;
      $newdata["needlist_linkman"]=$linkman;
      $newdata["needlist_linkmanphone"]=$tel;
      $newdata["needlist_desc"]=$desc;
      $newdata["needlist_timeinterval"]=$timeinterrval;
      $newdata["user_id"]=$userid;
      $newdata["address_name"]=$addname;
      $newdata["goodsclassify_id"]=$goodclassid;
      $newdata["goods_id"]=$goodid;
      $newdata["address_id"]=$addressid;

       $result=$addneeds->add($newdata);       
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"message"=>"发布成功","data"=>$result),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"发布失败","data"=>NULL),"JSON");
       }  

    }

    //查询卖家会员特权信息
    public function Privileges(){

       $privilege=M("Privilegelist");
       // $privilege=M("Supplylist");
       //添加条件查询
       $data=$privilege
       ->field("Privilegelist_id,t_user.user_id,supplylist_price,supplylist_id,supplylist_desc,supplylist_goodsname,address_name,user_nick,supplylist_minnumber,supplylist_time,supplylist_image") 

      ->join("t_user on t_privilegelist.user_id =t_user.user_id ")
      ->join("t_address on t_user.address_id=t_address.address_id")
      ->join("t_supplylist on t_privilegelist.user_id=t_supplylist.user_id")
       // ->where("privilegelist_id=1")
      ->select();
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }  
    }


    

}
//******************************之前写的接口*********************//

  // //用户采购中信息(查看详情)
    //   public function UserPurchasing($status){ 

    //    $mypurchaseid=$_POST["mypurchaseid"];

    //    $mypurchase=M("Mypurchase");

    //    $condition["mypurchase_id"]=$mypurchaseid;

    //    $data=$mypurchase
    //    ->field("mypurchase_status,t_mypurchase.user_id,t_needlist.address_id,t_needlist.goodsclassify_id,needlist_linkman,needlist_goodsname,needlist_time,needlist_desc,needlist_number")
    //    ->join("t_needlist on t_mypurchase.needlist_id =t_needlist.needlist_id ")
    //    ->join("t_user on t_mypurchase.user_id =t_user.user_id ")
    //    ->where($condition)
    //    ->select();
       
    //    if ($data) {
    //       $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
    //    }else{
    //         $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
    //    }
       
    // }

    // //用户采购完成信息
    //   public function FinishPurchase($status){
     
       
    // }


      // //查看用户id采购详情
    //   public function PurchaseDetail($userid){
     
       
    // }




    // //用户发布采购信息
    //   public function UserPurchase(){

    //    $purchaseid=$_POST["purchaseid"];
      
    //    $Purchase=M("Mypurchase");

    //    $condition["mypurchase_id"]=$purchaseid;
     
    //    //添加条件查询
    //    $data=$Purchase
    //    ->field("t_needlist.needlist_goodsname")
    //    ->join("t_needlist on t_mypurchase.needlist_id =t_needlist.needlist_id ")
  
    //    ->where($condition)

    //    ->select();
       
    //    if ($data) {
    //       $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
    //    }else{
    //         $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
    //    }
    // }
?>